using UnityEngine;
using System.Collections;

[RequireComponent(typeof(CharacterController))]
public class PlayerControler : MonoBehaviour {
	public float speed;
	private int count;
	public GUIText countText;
	public GUIText winText;
	private bool onGround;
	private Vector3 calibrationOffset;
	private Vector3 movement;
	private bool accelerometerControls;
	private bool OnAndroid;
	public bool DebugMode;
	public Material Menu1;
	public Material Menu2;
	public Material Menu3;
	
	#region Nicks Code
	public bool goal1;
	public bool goal2;
	public bool goal3;
	public bool goal4;
	#endregion
	
	#region Joystick Crap
	
	public Joystick MoveJoystick;
	public Joystick RotateJoystick;   
	#endregion   
	
	void Start() {
		count = 0;
		SetCountText ();
		winText.text = "";
		onGround = true;
		OnAndroid = Application.platform == RuntimePlatform.Android;
		if ( OnAndroid ) calibrationOffset = new Vector3 (Input.acceleration.x, 0, Input.acceleration.z);
		movement = new Vector3(0,0,0);
		accelerometerControls = false; //default to them off for now until there is a menu
		DebugMode = true;
		if(PlayerPrefs.GetInt("Texture") == 1)
			gameObject.renderer.material = Menu1;
		if(PlayerPrefs.GetInt("Texture") == 2)
			gameObject.renderer.material = Menu2;
		if(PlayerPrefs.GetInt("Texture") == 3)
			gameObject.renderer.material = Menu3;
		
		//MoveJoystick = gameObject.
	}
	
	void Update() {
		Jump (false);
		
		
	}
	
	void FixedUpdate() {
		Vector3 movement = new Vector3(0,0,0);
		if (OnAndroid && accelerometerControls) {
			movement = new Vector3 (Input.acceleration.x - calibrationOffset.x, 0, -Input.acceleration.z + calibrationOffset.z);
		} else if ((OnAndroid && !accelerometerControls) ){
			
		} else {
			float moveHorizontal = Input.GetAxis ("Horizontal");
			float moveVertical = Input.GetAxis ("Vertical");
			movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
		}
		
		if (DebugMode) {
		}
		
		rigidbody.AddForce (movement * speed * Time.deltaTime);
	}
	
	void OnTriggerEnter(Collider other) {
		/*if (other.gameObject.tag == "PickUp") {
                        other.gameObject.SetActive (false);
                        count++;
                        SetCountText();
                }*/
		if (other.gameObject.tag == "Ground") {
			onGround = true;
		}
		
		NicksTrigger (other);
	}
	
	//Nick's Code
	void NicksTrigger(Collider other) {
		if(other.gameObject.tag == "Ground2")
		{
			if(winText.text == "YOU WIN!!!!")
			{
				winText.text = "YOU'RE A TROOPER!!!!";
			}else{
				winText.text = "SECOND ONE!!!!";        
			}
			goal2 = true;
			onGround = true;
		}
		
		if(other.gameObject.tag == "Ground")
		{
			winText.text = "First One";
			onGround = true;
			goal1 = true;
		}
		if(other.gameObject.tag == "PickUp")
		{
			
			rigidbody.velocity -= other.gameObject.rigidbody.velocity;
		}
		
		if(other.gameObject.tag == "GroundOrigin")
		{
			if(goal2 && goal1 == true)
			{
				goal2 = false;
				goal1 = false;
				winText.text = "YOU WIN!";
				count++;
				SetCountText();
				
			}
			onGround = true;
		}
	}
	
	void SetCountText() {
		//Nick's Code
		countText.text = "YOU'VE WON " +count.ToString() + " TIMES!!!!!!";
	}
	
	#region GUI Stuff
	public void MoveUp() {
		rigidbody.AddForce (Vector3.forward * speed * Time.deltaTime);
	}
	
	public void MoveBack() {
		rigidbody.AddForce (Vector3.back * speed * Time.deltaTime);
	}
	
	public void MoveLeft() {
		rigidbody.AddForce (Vector3.left * speed * Time.deltaTime);
	}
	
	public void MoveRight() {
		rigidbody.AddForce (Vector3.right * speed * Time.deltaTime);
	}
	
	public void Jump(bool GUIButton) {
		bool IsTouched = false;
		if (OnAndroid && accelerometerControls) {
			IsTouched = Input.GetTouch (0).phase == TouchPhase.Began;
		}
		if (Input.GetButton ("Jump") || IsTouched || GUIButton) {
			//rigidbody.AddRelativeForce (Vector3.up * 3000 * Time.deltaTime );
			if (onGround) {
				rigidbody.velocity += Vector3.up * 300 * Time.deltaTime;
				onGround = false;
			}
		}
	}
	#endregion
	
	#region Joystick crap
	float joyStickInput (Joystick joystick) {
		Vector2 absJoyPos = new Vector2 (Mathf.Abs(joystick.position.x), Mathf.Abs(joystick.position.y));
		var xDirection = (joystick.position.x > 0) ? 1 : -1;
		var yDirection = (joystick.position.y > 0) ? 1 : -1;
		return ( ( absJoyPos.x > absJoyPos.y) ? absJoyPos.x * xDirection : absJoyPos.y * yDirection);
	}
	#endregion
	
}